import { useRef, useState, useEffect } from "react";
import { Button, message } from "antd";
import { PlusOutlined, ExclamationCircleOutlined } from "@ant-design/icons";
import { QueryPage } from "@components";
import { fields, GetColumns, useModalFields, GetModalFields } from "./config";
import { findCfgNotices, addOrEditCfgNotice, findTemplateByUnitCode } from "@apis/informConfigApi";
import { useMainContext } from '@contexts/MainContext'

const InformConfig = () => {
  const { getCode, isButtomShow } = useMainContext()

  const queryFormRef = useRef("queryFormRef");
  const formProps = {
    formRef: queryFormRef,
    fields: fields,
    initialValues: {
      status: '1',
    },
    extraTail: (
      <>
        {isButtomShow('430202') && <Button icon={<PlusOutlined />}
        onClick={() => {
          setIsDisabled(false)
          setIsInterior(false)
          modalRef.current.showModal()
        }}>
          新增
        </Button>}
      </>
    ),
  };

  //编辑
  const edit = (row) => {
    const { roleIds, tempType } = row
    setIsDisabled(true)
    setIsInterior(tempType === 'interior')
    const emerRoles = getCode('emerRoles').options
    const newValue = roleIds?.length === emerRoles?.length ? ['all'] : roleIds
    modalRef.current.showModal(row);
    setTimeout(() => {
      getTemplateList()
      modalFormRef.current?.setFieldValue('untrueRoleIds', newValue)
    }, 0);

  };

  const tableProps = {
    rowKey: "emerNoticeId",
    columns: GetColumns( queryFormRef, edit ),
  };

  const {modalFields, modalFormRef, setIsDisabled, getTemplateList } = GetModalFields()
  const [isInterior, setIsInterior] = useState(false)

  const modalRef = useRef("modalRef");
  const modalFormProps = {
    formRef: modalFormRef,
    fields: modalFields(isInterior, setIsInterior),
    initialValues: {
      status: '1',
      tempType: 'weatherBureau'
    },
  };

  return (
    <QueryPage
      formProps={formProps}
      tableProps={tableProps}
      queryApi={findCfgNotices}
      modalRef={modalRef}
      modalFormProps={modalFormProps}
      addApi={addOrEditCfgNotice}
      updataId="emerNoticeId"
      updataApi={addOrEditCfgNotice}
    />
  );
};

export default InformConfig;
